package com.xdbigdata.sync.utils;

import com.xdbigdata.sync.entity.Jiekou;
import lombok.extern.slf4j.Slf4j;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.apache.axis2.transport.http.HTTPConstants;
import org.springframework.stereotype.Component;

import javax.xml.namespace.QName;

/**
 * @author XD
 * @date 2019/9/19 18:37
 */
@Slf4j
@Component
public class RPCServiceClientUtil {
    public static RPCServiceClient client = null;


    public  String getStudData(Jiekou jiekou, Integer currPage) throws Exception {
        /***
         * 调用数据中心接口，通过学号查询学生信息
         */
        //服务发布的地址
        String url = jiekou.getUrl();
        //接口编号，用于对进口进行权限过滤
        String jkbh = jiekou.getJkbh();
        //用户名
        String userName = jiekou.getUserName();
        //密码
        String passWord = jiekou.getPassWord();
//        Integer currPage = jiekou.getCurrPage();
        //TODO 参数说明：<parame name=\"XH\" seq=\"0\" >"+xh+"</parame>   是查询条件，name="字段对应的数据库字段名称"
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><directions><interface>" + jkbh + "</interface><userName>" + userName + "</userName><password>" + passWord + "</password><currPage>" + currPage + "</currPage></directions>";
        String result = "";

        try {
            System.out.println("服务调用的地址：------------->>>" + url+xml);
            // 指定接口的名称空间和接口的方法名称insertAsyn是异步提交
            final QName qName = new QName("http://release.ly.com", "searchData");
            client = new RPCServiceClient();
            Options options = new Options();
            EndpointReference targetEPR = new EndpointReference(url);
            options.setTo(targetEPR);
            options.setTimeOutInMilliSeconds(600000L);
            options.setAction("searchData");
            options.setManageSession(true);
            options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT, true);options.setProperty(HTTPConstants.SO_TIMEOUT, 600000);
            client.setOptions(options);
            long begin = System.currentTimeMillis();
            client.getOptions().setTimeOutInMilliSeconds(600000L);
            Object[] rtnObjs = client.invokeBlocking(qName, new Object[]{xml,
                    "", ""}, new Class[]{String.class});
            result = rtnObjs[0].toString();
            long end = System.currentTimeMillis();
            System.out.println("接口调用耗时：" + (end - begin) + " 毫秒");
            System.out.println("调用结果----->>" + result.toString());
            client.cleanupTransport();
        } catch (AxisFault e) {
            e.printStackTrace();
            log.error(e.getMessage());
            System.err.println("远程服务异常或没启动:" + e.getMessage());
            StringBuffer bf = new StringBuffer();
            bf.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
            bf.append("<directions>");
            bf.append("<result>0</result>");
            bf.append("</directions>");
            result = bf.toString();
        } finally {
            // 必须释放资源否则出错  ,解决当调用失败是，Timeout waiting for connection 的错
            try {
                client.cleanupTransport();
            } catch (AxisFault e) {
                e.printStackTrace();
            }
        }
        return result.toString();
    }
}
